import {
  Drawer,
  Dialog,
  TDrawerOption,
  TDialogOption
} from "@/components/model";
import { ElMessage, MessageParams } from "element-plus";
export class Helper {
  private static hasMessage = false;
  static staticDrawers: Drawer;
  static staticDialog: Dialog;
  static showMessage(
    message: string,
    type?: "success" | "warning" | "error" | "message"
  ) {
    const option: MessageParams = {
      message,
      type: (type as any) || "success",
      onClose: () => {
        Helper.hasMessage = false;
      }
    };
    if (!Helper.hasMessage) {
      Helper.hasMessage = true;
      ElMessage(option);
    }
  }

  static showDrawer(option: TDrawerOption) {
    return new Drawer(option, true).of();
  }

  static showStaticDrawer(option: TDrawerOption) {
    if (!Helper.staticDrawers) {
      Helper.staticDrawers = new Drawer(option);
    } else {
      Helper.staticDrawers.open();
    }
    return Helper.staticDrawers.of();
  }

  static showDialog(option: TDialogOption) {
    return new Dialog(option, true).of();
  }

  static showStaticDialog(option: TDialogOption) {
    if (!Helper.staticDialog) {
      Helper.staticDialog = new Dialog(option);
    } else {
      Helper.staticDialog.open();
    }
    return Helper.staticDialog.of();
  }

  static destoryModel() {
    const model = document.getElementsByClassName("drawer_dailog");
    if (model) {
      Array.from(model).forEach(r => {
        r.remove();
      });
    }
    Helper.staticDrawers = null;
    Helper.staticDialog = null;
  }
}
